package com.xts.weinfo.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xts.weinfo.R;
import com.xts.weinfo.bean.NaviBean;
import com.xts.weinfo.ui.activity.NaviActivity;
import com.xts.weinfo.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

public class RlvNaviAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList<NaviBean.DataBean> mList;
    private final LayoutInflater mInflater;
    private int mRed = Color.parseColor("#ff0000");
    private int mGreen = Color.parseColor("#00ff00");
    private int mBlue = Color.parseColor("#0000ff");

    public RlvNaviAdapter(Context context, ArrayList<NaviBean.DataBean> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_navi, parent, false);
        return new VH(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh = (VH) holder;
        List<NaviBean.DataBean.ArticlesBean> articles = mList.get(position).getArticles();
        for (int i = 0; i < articles.size(); i++) {
            TextView tv = (TextView) mInflater.inflate(R.layout.item_label, null, false);
            tv.setText(articles.get(i).getTitle());
            if (i % 3 == 0){
                tv.setTextColor(mRed);
            }else if (i % 3 == 1){
                tv.setTextColor(mGreen);
            }else {
                tv.setTextColor(mBlue);
            }
            vh.mFl.addView(tv);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<NaviBean.DataBean> data) {
        mList.addAll(data);
        notifyDataSetChanged();
    }

    class VH extends RecyclerView.ViewHolder {

        private final FlowLayout mFl;

        public VH(View itemView) {
            super(itemView);
            mFl = itemView.findViewById(R.id.fl);
        }
    }
}
